package games;

import teams.Team;

public class CupMatch extends Match {

    // ** Variables **//
    private SimpleMatch simpleMatch1;

    private SimpleMatch simpleMatch2;

    private int penaltyGoals1 = 0;

    private int penaltyGoals2 = 0;

    // ** Constructores **//
    /**
     * Constructor que te permite crear el partido de copa sin penales.
     */
    public CupMatch(final SimpleMatch simpleMatch1, final SimpleMatch simpleMatch2) {
        this.setTeam1(simpleMatch1.getTeam1());
        this.setTeam2(simpleMatch1.getTeam2());
        this.simpleMatch1 = simpleMatch1;
        this.simpleMatch2 = simpleMatch2;
    }

    /**
     * Constructor que te permite crear el partido de copa con penales.
     */
    public CupMatch(final SimpleMatch simpleMatch1, final SimpleMatch simpleMatch2, final int penaltyGoals1,
            final int penaltyGoals2) {
        this.setTeam1(simpleMatch1.getTeam1());
        this.setTeam2(simpleMatch1.getTeam2());
        this.simpleMatch1 = simpleMatch1;
        this.simpleMatch2 = simpleMatch2;
        this.penaltyGoals1 = penaltyGoals1;
        this.penaltyGoals2 = penaltyGoals2;
    }

    /**
     * Constructor vacio utilizado para el Hibernate.
     */
    protected CupMatch() {
    }

    @Override
    /**
     * Este metodo te retorna el Team ganador
     * @return Team
     */
    public Team getWinner() {
        int goals1 = this.simpleMatch1.getGoals1() + this.simpleMatch2.getGoals1();
        int goals2 = this.simpleMatch1.getGoals2() + this.simpleMatch2.getGoals2();

        if (goals1 > goals2) {
            return this.getTeam1();
        } else if (goals1 < goals2) {
            return this.getTeam2();
        }
        return this.getPenaltyWinner();
    }

    public boolean havePenalty() {
        int goals1 = this.simpleMatch1.getGoals1() + this.simpleMatch2.getGoals1();
        int goals2 = this.simpleMatch1.getGoals2() + this.simpleMatch2.getGoals2();
        return goals1 == goals2;
    }

    /**
     * Retorna el Team ganador de los penales.
     * 
     * @return Team
     */
    private Team getPenaltyWinner() {
        if (this.penaltyGoals1 > this.penaltyGoals2) {
            return this.getTeam1();
        } else {
            return this.getTeam2();
        }
    }

    // ** Getters y Setters **//
    public SimpleMatch getSimpleMatch1() {
        return this.simpleMatch1;
    }

    public void setSimpleMatch1(final SimpleMatch simpleMatch1) {
        this.simpleMatch1 = simpleMatch1;
    }

    public SimpleMatch getSimpleMatch2() {
        return this.simpleMatch2;
    }

    public void setSimpleMatch2(final SimpleMatch simpleMatch2) {
        this.simpleMatch2 = simpleMatch2;
    }

    public int getPenaltyGoals1() {
        return this.penaltyGoals1;
    }

    public void setPenaltyGoals1(final int penaltyGoals1) {
        this.penaltyGoals1 = penaltyGoals1;
    }

    public int getPenaltyGoals2() {
        return this.penaltyGoals2;
    }

    public void setPenaltyGoals2(final int penaltyGoals2) {
        this.penaltyGoals2 = penaltyGoals2;
    }

}
